package com.catmiw.wyz.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class DigestUtils {
	public static String toHexString(byte[] b){
		if (b==null)
			return null;
		StringBuilder sb=new StringBuilder();
		for (byte x:b){
			String tt=Integer.toHexString(x&0xff);
			sb.append("00".substring(tt.length())).append(tt);
		}
		return sb.toString();
	}
	
	public static byte[] md5(String str){
		MessageDigest md;
		byte b[]=null;
		try {
			md = MessageDigest.getInstance("MD5");
			md.update(str.getBytes());
			b = md.digest();
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return b;
	}
	
	public static String md5Hex(String str){
		return toHexString(md5(str));
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String md=DigestUtils.md5Hex("test");
		System.out.println(md);
	}

}
